<?php


namespace App\Exports\SearchStage;

use App\Exceptions\ApiException;
use App\Models\PartCompany;
use App\Models\ProjectClassify;
use App\Services\BaseService;
use App\Services\CommonService;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
use Maatwebsite\Excel\Concerns\WithCustomStartCell;
use Maatwebsite\Excel\Concerns\WithStyles;
use Maatwebsite\Excel\Concerns\WithTitle;
use PhpOffice\PhpSpreadsheet\Exception as PhpSpreadsheetException;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

/**
 * Class BookTitleExport29
 * @package App\Exports
 * 按照单位工程 29号案卷目录导出格式
 */
class UpBookTitleExport29 implements FromCollection, WithTitle, WithColumnWidths, WithStyles, WithCustomStartCell
{
    use Exportable;

    protected $column;
    protected $table;
    protected $ids;
    protected $partCompanyId;
    protected $projectClassifyId;
    protected $partCompanyName;
    protected $proClassName;

    public function __construct($table, $ids)
    {
        $this->table             = $table;
        $this->ids               = $ids;
        $tableParam              = CommonService::getTableParams($this->table);
        $this->partCompanyId     = $tableParam['id1'];
        $this->projectClassifyId = $tableParam['id2'];
        $this->partCompanyName   = PartCompany::query()->where('id', $this->partCompanyId)->value('title');
        $this->proClassName      = ProjectClassify::query()->where('id', $this->projectClassifyId)->value('title');
    }

    /**
     * 获取数据集合
     *
     * @return Collection
     * @throws ApiException
     * @author: lxk
     * @date: 2024/3/2 11:45
     */
    public function collection(): Collection
    {
        $r_list = [];
        $number = 1;
        $ids    = $this->ids;
        $list   = CommonService::getBookTitleInstance($this->partCompanyId, $this->projectClassifyId)
            ->when($ids[0], function ($query) use ($ids) {
                return $query->whereIn('id', $ids);
            })
            ->select('id', 'archives_no', 'title', 'word_material', 'blueprint', 'page_num', 'store_period', 'start_date', 'end_date')
            ->orderBy('serial_no')
            ->get();
        if ($list->isNotEmpty()) {
            $list = $list->toArray();
            foreach ($list as $val) {
                $arr['number']           = $number++;              //序号
                $arr['archives_no']      = $val['archives_no'];   //档案号
                $arr['title']            = $val['title'];         //标题
                $arr['word_material']    = $val['word_material']; //文字材料
                $arr['blueprint']        = $val['blueprint'];     //蓝图
                $arr['total_page']       = $val['page_num'];      //总页数
                $arr['store_period']     = $val['store_period'];  //保管期限
                $arr['start_end_date']   = format_date($val['start_date'], $val['end_date']); //起止日期
                $arr['project_classify'] = $this->proClassName;   //单位工程
                $arr['part_company']     = $this->partCompanyName;//形成单位
                $r_list[]                = $arr;
            }
        }
        $this->column = count($r_list) + 3;
        return collect($r_list);
    }

    /**
     * sheet名称
     * @return string
     */
    public function title(): string
    {
        return $this->proClassName . '(id=' . $this->projectClassifyId . ')';
    }

    /**
     * 设置列宽
     * @return array
     */
    public function columnWidths(): array
    {
        return [
            'A' => 10, 'B' => 25, 'C' => 60, 'D' => 6, 'E' => 8, 'F' => 6, 'G' => 10, 'H' => 20, 'I' => 30, 'J' => 40
        ];
    }

    /**
     * 从哪个单元格开始渲染数据，数据集合如 $list
     * @return string
     */
    public function startCell(): string
    {
        return 'A4';
    }

    /**
     * 样式设置
     * @param Worksheet $sheet
     * @throws PhpSpreadsheetException
     */
    public function styles(Worksheet $sheet)
    {
        $sheet->getDefaultRowDimension()->setRowHeight(33); //设置行高(全部行高)
        $sheet->getStyle('A1:J' . $this->column)->getFont()->setName('宋体'); //宋体
        $sheet->getStyle('C4:J' . $this->column)->getFont()->setSize(10); //大小

        $sheet->mergeCells('A1:J1');
        $sheet->setCellValue('A1', '案卷目录');

        $sheet->mergeCells('A2:A3'); //序号
        $sheet->mergeCells('B2:B3'); //档案号
        $sheet->mergeCells('C2:C3'); //案卷题名
        $sheet->mergeCells('D2:F2'); //案卷页数
        $sheet->mergeCells('G2:G3'); //保管期限
        $sheet->mergeCells('H2:H3'); //起止日期
        $sheet->mergeCells('I2:I3'); //形成单位
        $sheet->mergeCells('J2:J3'); //单位工程

        /**
         * 属性名设置
         */
        $sheet->setCellValue('A2', '序号');
        $sheet->setCellValue('B2', '档案号');
        $sheet->setCellValue('C2', '案卷题名');
        $sheet->setCellValue('D2', '案卷页数');
        $sheet->setCellValue('D3', '文字材料');
        $sheet->setCellValue('E3', '蓝图');
        $sheet->setCellValue('F3', '总页数');
        $sheet->setCellValue('G2', '保管期限');
        $sheet->setCellValue('H2', '起止日期');
        $sheet->setCellValue('I2', '单位工程');
        $sheet->setCellValue('J2', '形成单位');

        /**
         * 顶部案卷目录样式设置
         */
        $sheet->getStyle('A1:J' . $this->column)->getAlignment()->setVertical('center');//垂直居中
        $sheet->getStyle('A1:J' . $this->column)->getAlignment()->setHorizontal('center');//水平居中
        $sheet->getStyle('B4:C' . $this->column)->getAlignment()->setHorizontal('left');//左对齐
        $sheet->getStyle('C4:C' . $this->column)->getAlignment()->setWrapText(true); //自动换行
        $sheet->getStyle('D3:F3')->getAlignment()->setWrapText(true); //自动换行

        $sheet->getStyle('A1')->getFont()->setSize(20); //大小
        $sheet->getStyle('A1:J2')->getFont()->setBold(true); //粗体
        $sheet->getStyle('A2:J2')->getFont()->setSize(11); //大小
        $sheet->getStyle('D3:F3')->getFont()->setSize(11); //大小
        $sheet->getStyle('D3:F3')->getFont()->setBold(true); //粗体

        /**
         * 设置边框
         */
        $styleArray = [
            'borders' => [
                'allBorders' => [
                    'borderStyle' => Border::BORDER_THIN //细边框
                ]
            ]
        ];
        $sheet->getStyle('A1:J' . $this->column)->applyFromArray($styleArray);
    }

}
